package com.css.zfzx.fwsjcj.modules.taskmanage.taskfrom.repository;

import com.css.zfzx.fwsjcj.commen.util.Constant;
import com.css.zfzx.fwsjcj.modules.taskmanage.taskfrom.entity.FwFormEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Repository
public interface TaskFormRepository extends JpaRepository<FwFormEntity, String>, JpaSpecificationExecutor<FwFormEntity> {
    @Modifying
    @Query(nativeQuery = true, value = "update fw_form set sp_type = :spType where fw_id in :ids")
    void subAll(@Param("spType") String spType, @Param("ids") List<String> ids);

    @Modifying
    @Query(nativeQuery = true, value = "update fw_form set sp_type = :spType, submit_time = :submitTime where fw_id in :ids")
    void subAllByIds(@Param("spType") String spType, @Param("ids") List<String> ids,  @Param("submitTime") Date submitTime);

    @Modifying
    @Query(nativeQuery = true, value = "update fw_form set is_valid ='" + Constant.IS_VALID_NO + "'where fw_id in :ids")
    void delByIds(@Param("ids") List<String> ids);

    FwFormEntity findByFormId(String formId);

    @Query(nativeQuery = true, value = "select count(creat_user_id) from fw_form where creat_user_id =:userId and sp_type =" + Constant.SP_YES + " and is_valid =" + Constant.IS_VALID_YES)
    Long findByCount(@Param("userId") String userId);

    @Modifying
    @Query(nativeQuery = true, value = "update fw_form set reason =:reason,sp_type =:status,check_time =:checkTime where form_id =:formId")
    void spForm(@Param("status") String status, @Param("reason") String reason, @Param("formId") String formId, @Param("checkTime") Date checkTime);

    @Query(nativeQuery = true, value = "select * from fw_form where is_valid =" + Constant.IS_VALID_YES + " order by task_id")
    List<FwFormEntity> findAllValid();

    @Query(nativeQuery = true, value = "select * from fw_form where is_valid =" + Constant.IS_VALID_YES + " and sp_type =" + Constant.SP_YES + " and building_address like %?1%")
    List<FwFormEntity> findLike(String address);
    @Query(nativeQuery = true, value = "select * from fw_form where is_valid =" + Constant.IS_VALID_YES + " and sp_type =" + Constant.SP_YES )
    List<FwFormEntity> findAllCountry();

    @Query(nativeQuery = true, value = "select * from fw_form where is_valid=" + Constant.IS_VALID_YES + " and building_name like concat('%',:buildingName,'%')")
    List<FwFormEntity> findLikeBuidingName(String buildingName);

    /**
     * 根据普查表类型查询
     */
    @Query(nativeQuery = true, value =
            "select form_id from fw_form where is_valid=" + Constant.IS_VALID_YES +
                    " and sp_type =" + Constant.SP_YES +
                    " and if(:buildingType!='', buliding_type =:buildingType, 1 = 1)" +
                    " and if(:startDate is not null, creat_time >= :startDate, 1 = 1) " +
                    " and if(:endDate is not null ,creat_time <= :endDate, 1 = 1) " +
                    " and if(:province != '', belongto_province =:province, 1=1)" +
                    " order by creat_time desc")
    List<String> findFormIdListBybuildingType(@Param("buildingType") String buildingType,
                                              @Param("startDate") Date startDate,
                                              @Param("endDate") Date endDate,
                                              @Param("province") String province);

    @Query(nativeQuery = true, value = "select distinct belongto_province from fw_form where is_valid=" + Constant.IS_VALID_YES + " order by creat_time desc")
    List<String> listProvince();

    @Query(nativeQuery = true, value = "select distinct belongto_city from fw_form where is_valid=" + Constant.IS_VALID_YES + " order by creat_time desc")
    List<String> listCity();

    @Query(nativeQuery = true, value = "select division_code from org_division where division_name= :buildingArea")
    String getDivisionCode(@Param("buildingArea")String buildingArea);

    @Query(nativeQuery = true, value = "select user_id userId,user_name userName from org_user where user_code= :userCode")
    Map<String,Object> getUserInfo(@Param("userCode")String userCode);

    @Modifying
    @Query(nativeQuery = true, value = "update fw_form set sp_type = :spType , back_opinion=:backOpinion where fw_id in :idList")
    void backAll(@Param("spType")String spType, @Param("backOpinion")String backOpinion, @Param("idList")List<String> idList);
}
